<template>
  <div>
    <el-dialog :visible="visible" :title="formDate.id?'编辑一体杆':'添加一体杆'" width="550px" @close="$emit('update:visible',false)">
      <el-row type="flex" justify="center">
        <el-form ref="formRef" size="mini" :model="formDate" :rules="rules">
          <el-form-item label="一体杆名称" prop="poleName">
            <el-input v-model="formDate.poleName" placeholder="请输入一杆名称" style="width:320px" />
          </el-form-item>
          <el-form-item label="一体杆编号" prop="poleNumber">
            <el-input v-model="formDate.poleNumber" :disabled="!!formDate.id" placeholder="请输入一体杆编号" style="width:320px" />
          </el-form-item>
          <el-form-item label="一体杆IP" prop="poleIp">
            <el-input v-model="formDate.poleIp" placeholder="请输入一体杆IP" style="width:320px" />
          </el-form-item>
          <el-form-item label="关联区域" prop="areaId">
            <el-select v-model="formDate.areaId" :data="list" placeholder="请输入关联区域" style="width:320px">
              <el-option v-for="item in list" :key="item.areaId" :value="item.areaId" :label="item.areaName" /></el-select>
          </el-form-item>
          <el-form-item label="一体杆类型" prop="poleType">
            <el-select v-model="formDate.poleType" placeholder="请输入一体杆类型" style="width:320px">

              <el-option value="entrance" label="入口">入口</el-option>
              <el-option value="export" label="出口">出口</el-option>

            </el-select>
          </el-form-item>
          <el-row type="flex" justify="end">
            <el-form-item>
              <el-button @click="$emit('update:visible', false)">取消</el-button>
              <el-button size="medium" type="primary" @click="onSubmit">确定</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { addIeceMange, editorIeceAPI, getListAreaAPI } from '@/api/ieceManage'
export default {
  props: {
    visible: Boolean
  },
  data() {
    return {
      list: [], // 渲染下拉区域
      formDate: {
        poleName: '',
        poleNumber: '',
        poleIp: '',
        areaId: null,
        poleType: '',
        id: null
      },
      rules: {
        poleName: [
          { required: true, message: '请输入一杆名称', trigger: 'blur' }
        ],
        poleNumber: [
          { required: true, message: '请输入一体杆编号', trigger: 'blur' }
        ],
        poleIp: [
          { required: true, message: '请输入一体IP', trigger: 'blur' },
          { pattern: /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/, message: '请输入正确的IP', trigger: 'blur' }
        ],
        areaId: [
          { required: true, message: '请选择关联区域', trigger: 'blur' }
        ],
        poleType: [
          { required: true, message: '请输入一杆类型', trigger: 'blur' }
        ]
      }
    }
  },
  // 重置
  watch: {
    visible(newVisible) {
      if (!newVisible) {
        this.$refs.formRef.resetFields()
      }
    }
  },
  async created() {
    // 一打开添加弹出层，发请求拿数据，渲染下拉列表;
    const res = await getListAreaAPI()
    // console.log(res)
    this.list = res.data
    // this.formDate.areaId = res.data[0].areaId
  },
  methods: {
    async onSubmit() {
      // 点击确定，校验表单
      this.$refs.formRef.validate()
      // 发送添加请求 通知父组件刷新页面 提示用户 关闭弹出层
      if (!this.formDate.id) {
        await addIeceMange(this.formDate)
        this.$message.success('添加成功')
      } else {
        await editorIeceAPI(this.formDate)
        this.$message.success('编辑成功')
      }

      this.$emit('update:visible', false)
      //   通知父组件刷新页面
      this.$emit('onSuccess')
    }
  }
}
</script>

<style>

</style>
